package com.lwx.simpleapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.util.TypedValue;
import android.view.TextureView;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class CameraActivity extends AppCompatActivity {

    TextureView textureView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        textureView = findViewById(R.id.texture);


    }


    @Override
    protected void onResume() {
        super.onResume();
        ImageView imageview = findViewById(R.id.imageview);
        BitmapFactory.Options options = new BitmapFactory.Options();
        TypedValue value = new TypedValue();
        options.inTargetDensity = value.density;
        options.inScaled=true;//不缩放
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bito);

        imageview.setImageBitmap(blurBitmap(this,bitmap,25));
    }

    public Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
        // 计算图片缩小后的长宽
        int width = Math.round(image.getWidth() * 1);
        int height = Math.round(image.getHeight() * 1);

        // 将缩小后的图片做为预渲染的图片
        Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
        // 创建一张渲染后的输出图片
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

        // 创建RenderScript内核对象
        RenderScript rs = RenderScript.create(context);
        // 创建一个模糊效果的RenderScript的工具对象
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间
        // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

        // 设置渲染的模糊程度, 25f是最大模糊度
        blurScript.setRadius(blurRadius);
        // 设置blurScript对象的输入内存
        blurScript.setInput(tmpIn);
        // 将输出数据保存到输出内存中
        blurScript.forEach(tmpOut);

        // 将数据填充到Allocation中
        tmpOut.copyTo(outputBitmap);

        return outputBitmap;
    }



    void openCamere() throws CameraAccessException {
        CameraManager cameraManager;
        cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

        String frontCameraId;
        String[] cameraIdList = cameraManager.getCameraIdList();
        CameraCharacteristics frontCameraCharacteristics = null;
        //获取前置摄像头的ID
        for (int i = 0; i < cameraIdList.length; i++) {
            frontCameraCharacteristics = cameraManager.getCameraCharacteristics(cameraIdList[i]);
            int facing = frontCameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
            if(facing == CameraCharacteristics.LENS_FACING_FRONT){
                frontCameraId = cameraIdList[i];
                break;
            }
        }

        //摄像头方向
        int oriention = frontCameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);

        //摄像头支持的数据格式
        StreamConfigurationMap confiurationMap = frontCameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);




    }
}
